package com.hllxd.hism.star.basic.service.controller2;

import com.hllxd.hism.framework.httprpc.service.*;
import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.model.dapan.QuotaEntity;
import com.hllxd.hism.star.basic.service.service.DapanService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * web大盘数据
 */
@Slf4j
@RestGrpcService
@RestGrpcMapping("/v1/web_dapan")
public class WebDapanController2 {

    @Autowired
    private DapanService dapanService;

    @Data
    public static class Param {
        public String bizTime;
        public String startDt;
        public String endDt;
        /***/
        public String biz;
        public String appDiv;
        public String platform;
        public String zgroup;

        public String dimId;
        public String dimValue;
        public String indexCn;
        public String indexEn;
        public String dimValueCn;
    }


    /**
     * [web使用]获取大盘下指标（层级关系）
     *
     * @param params 参数
     * @param result 结果
     * @return 指标详情
     */
    @RestGrpcMapping("/listWebMetricAll.do")
    public NTSTATUS listWebMetricAll(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            if (aa.getBizTime() == null || "".equals(aa.getBizTime())) {
                String yesterday = LocalDate.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyyMMdd"));
                aa.setBizTime(yesterday);
            }
            List<QuotaEntity> quotaEntities = dapanService.listWebMetricAll(aa.bizTime, null, aa.getBiz(), aa.getAppDiv(), aa.getPlatform());
            result.setData(quotaEntities);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    /**
     * [web使用]获取appdiv下所有大盘趋势
     *
     * @param params 参数
     * @param result 结果
     * @return 大盘趋势map
     */
    @RestGrpcMapping("/listAllMetricSeries.do")
    public NTSTATUS listAllMetricSeries(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            if (aa.getBizTime() == null || "".equals(aa.getBizTime())) {
                String yesterday = LocalDate.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyyMMdd"));
                aa.setBizTime(yesterday);
            }
            String startDt = LocalDate.now().minusDays(31).format(DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00"));
            String endDt = LocalDate.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00"));
            aa.setStartDt(startDt);
            aa.setEndDt(endDt);

            log.info("dt: {}, {}, biz: {}, appdiv: {}.", aa.startDt, aa.endDt, aa.biz, aa.appDiv);
            result.setData(dapanService.listAllMetricSeries(aa.startDt, aa.endDt, aa.biz, aa.appDiv, aa.getPlatform()));
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }


    /**
     * [web使用]获取appdiv下大盘维度配置数据
     *
     * @param params 参数
     * @param result 结果
     * @return 大盘趋势map
     */
//    @RestGrpcMapping("/listDashboardDimData.do")
//    public NTSTATUS listDashboardDimData(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
//        log.info("request params: {}", params);
//        try {
//            Param aa = params.driverObject(Param.class);
//            if (aa.getBizTime() == null || "".equals(aa.getBizTime())) {
//                String yesterday = LocalDate.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyyMMdd"));
//                aa.setBizTime(yesterday);
//            }
//            result.setData(dapanService.listDashboardDimData(aa.getBizTime(), aa.getAppDiv(), aa.getBiz(), aa.getDimId()));
//            return NTSTATUS.SUCCESS_STATUS();
//        } catch (BusinessException e) {
//            log.warn("Exception: {}. {}", e, params);
//            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
//        } catch (Exception e) {
//            log.error("Exception: {}. {}", e, params, e);
//            return NTSTATUS.ERROR_STATUS("500", e.toString());
//        }
//    }

    /**
     * [web使用]获取appdiv下维度下拉框
     *
     * @param params 参数
     * @param result 结果
     * @return 大盘趋势map
     */
//    @RestGrpcMapping("/listDapanDimConfig.do")
//    public NTSTATUS listDapanDimConfig(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
//        log.info("request params: {}", params);
//        try {
//            Param aa = params.driverObject(Param.class);
//            if (aa.getBizTime() == null || "".equals(aa.getBizTime())) {
//                String yesterday = LocalDate.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyyMMdd"));
//                aa.setBizTime(yesterday);
//            }
//            result.setData(dapanService.listDapanDimConfig(aa.getBizTime(), aa.getAppDiv(), aa.getBiz()));
//            return NTSTATUS.SUCCESS_STATUS();
//        } catch (BusinessException e) {
//            log.warn("Exception: {}. {}", e, params);
//            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
//        } catch (Exception e) {
//            log.error("Exception: {}. {}", e, params, e);
//            return NTSTATUS.ERROR_STATUS("500", e.toString());
//        }
//    }


    /**
     * [web使用]获取维度下指标趋势图信息
     *
     * @param params 参数
     * @param result 结果
     * @return 趋势图信息
     */
//    @RestGrpcMapping("/getDimIndexChart.do")
//    public NTSTATUS getDimIndexChart(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
//        log.info("request params: {}", params);
//        try {
//            Param aa = params.driverObject(Param.class);
//            result.setData(dapanService.getDimIndexChart(aa.getDimId(), aa.getDimValue(), aa.getIndexEn(), aa.getIndexCn(), aa.getDimValueCn(),aa.getBiz()));
//            return NTSTATUS.SUCCESS_STATUS();
//        } catch (BusinessException e) {
//            log.warn("Exception: {}. {}", e, params);
//            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
//        } catch (Exception e) {
//            log.error("Exception: {}. {}", e, params, e);
//            return NTSTATUS.ERROR_STATUS("500", e.toString());
//        }
//    }

}
